//
//  PictureShower.m
//  PictureShower
//
//  Created by 宫 酉昊 on 15/6/2.
//  Copyright (c) 2015年 宫 酉昊. All rights reserved.
//

#import "PictureShower.h"
#import "PictureBackgroundView.h"
static CGRect oldframe;
@interface PictureShower()
@end
@implementation PictureShower
+ (void)showImage:(UIImageView *)avatarImageView{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
    
    CGRect backgroundImageFrame;
    float mutiplyFactor;
    if (([UIScreen mainScreen].bounds.size.height / oldframe.size.height) >= ([UIScreen mainScreen].bounds.size.width / oldframe.size.width)) {
        mutiplyFactor = [UIScreen mainScreen].bounds.size.height / oldframe.size.height;
    }else{
        mutiplyFactor = [UIScreen mainScreen].bounds.size.width / oldframe.size.width;
    }
    backgroundImageFrame = CGRectMake(-(oldframe.size.width*mutiplyFactor-[UIScreen mainScreen].bounds.size.width)/2, -(oldframe.size.height*mutiplyFactor-[UIScreen mainScreen].bounds.size.height)/2, oldframe.size.width*mutiplyFactor, oldframe.size.height*mutiplyFactor);
    UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:backgroundImageFrame];
    backgroundImageView.image=avatarImageView.image;
    backgroundImageView.tag = 222;
    backgroundImageView.transform = CGAffineTransformMakeRotation(M_PI*2);
    backgroundImageView.alpha = 0;
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blur];
    blurView.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.1];
    blurView.frame = backgroundImageView.frame;
    [backgroundImageView addSubview:blurView];
    
    PictureBackgroundView* backgroundView = [[PictureBackgroundView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) andImageView:avatarImageView];
    backgroundView.backgroundColor=[UIColor clearColor];
    backgroundView.alpha=0;
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 1;
        backgroundImageView.alpha = 1;
    }];
    
    [window addSubview:backgroundImageView];
    [window addSubview:backgroundView];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];
}

+(void)hideImage:(UITapGestureRecognizer*)tap{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    PictureBackgroundView *backgroundView = (PictureBackgroundView*)tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    UIImageView *backgroundImageView = (UIImageView*)[tap.view.window viewWithTag:222];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0;
        backgroundImageView.alpha = 0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
        [backgroundImageView removeFromSuperview];
    }];
}
@end

